import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useTagStore = defineStore('tagStore', () => {
    // 标签列表
    const tags = ref([
        {
            path: '/admin/index',
            name: '仪表盘',
            closable: false // 首页不可关闭
        }
    ])

    // 添加标签
    const addTag = (tag) => {
        // 检查标签是否已存在
        const exists = tags.value.find(item => item.path === tag.path)
        if (!exists) {
            tags.value.push({
                path: tag.path,
                name: tag.name,
                closable: true
            })
        }
    }

    // 删除标签
    const removeTag = (path) => {
        const index = tags.value.findIndex(item => item.path === path)
        if (index !== -1) {
            tags.value.splice(index, 1)
        }
    }

    // 关闭所有标签（除了首页）
    const closeAllTags = () => {
        tags.value = tags.value.filter(tag => !tag.closable)
    }

    // 关闭其他标签
    const closeOtherTags = (currentPath) => {
        tags.value = tags.value.filter(tag => tag.path === currentPath || !tag.closable)
    }

    // 清空所有标签
    const clearTags = () => {
        tags.value = [
            {
                path: '/admin/index',
                name: '仪表盘',
                closable: false
            }
        ]
    }

    return {
        tags,
        addTag,
        removeTag,
        closeAllTags,
        closeOtherTags,
        clearTags
    }
}, {
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'tag-store',
                storage: localStorage,
                paths: ['tags']
            }
        ]
    }
})

